package com.china08.yxyapi.api.home.kecheng.sync;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroGrade;
import com.china08.yxyapi.entity.db.res.YxyResVersion;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroGradeRepository;
import com.china08.yxyapi.repository.db.YxyResVersionRepository;

/**
 * 同步课程--列表页--通过学段、学科筛选版本、分类
 * 
 * @author LiuWeidong
 *
 */

@CrossOrigin
@RestController
public class KechengSyncPhaceSubjectListController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsSynchroCatalogRepository yxyLessonsSynchroCatalogRepository;

    /** 版本 */
    @Autowired
    private YxyResVersionRepository yxyResVersionRepository;

    /** 分类 */
    @Autowired
    private YxyLessonsSynchroGradeRepository yxyLessonsSynchroGradeRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/sync/phace/subject/lst/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel model) {

        String stageId = model.stageId;
        String subjectId = model.subjectId;

        List<RespModel> returnLst = new ArrayList<>();
        RespModel aa = new RespModel();
        aa.versionId = "";
        aa.versioName = "不限";
        aa.gradeId = "";
        aa.gradeName = "不限";
        returnLst.add(aa);

        if (subjectId != null && subjectId != "") {
            // 版本
            List<YxyLessonsSynchroCatalog> versionLst = yxyLessonsSynchroCatalogRepository.findByVersionIdAll2(stageId,
                    subjectId);

            for (YxyLessonsSynchroCatalog yxyLessonsSynchroCatalog : versionLst) {
                RespModel model1 = new RespModel();
                model1.versionId = yxyLessonsSynchroCatalog.getVersionId();

                YxyResVersion yxyResVersions = yxyResVersionRepository.findById(model1.versionId);
                model1.versioName = yxyResVersions.getName();
                returnLst.add(model1);
            }

            // 分类
            List<YxyLessonsSynchroCatalog> gradeLst = yxyLessonsSynchroCatalogRepository.findByGradeIdAll2(stageId,
                    subjectId);
            for (YxyLessonsSynchroCatalog yxyLessonsSynchroCatalog : gradeLst) {
                RespModel model1 = new RespModel();
                model1.gradeId = yxyLessonsSynchroCatalog.getGradeId();
                YxyLessonsSynchroGrade yxyLessonsSynchroGrades = yxyLessonsSynchroGradeRepository
                        .findById(model1.gradeId);
                model1.gradeName = yxyLessonsSynchroGrades.getName();
                returnLst.add(model1);
            }
        } else {
            // 版本
            List<YxyLessonsSynchroCatalog> versionLst = yxyLessonsSynchroCatalogRepository.findByVersionIdAll1(stageId);

            for (YxyLessonsSynchroCatalog yxyLessonsSynchroCatalog : versionLst) {
                RespModel model1 = new RespModel();
                model1.versionId = yxyLessonsSynchroCatalog.getVersionId();

                YxyResVersion yxyResVersions = yxyResVersionRepository.findById(model1.versionId);
                model1.versioName = yxyResVersions.getName();
                returnLst.add(model1);
            }

            // 分类
            List<YxyLessonsSynchroCatalog> gradeLst = yxyLessonsSynchroCatalogRepository.findByGradeIdAll1(stageId);
            for (YxyLessonsSynchroCatalog yxyLessonsSynchroCatalog : gradeLst) {
                RespModel model1 = new RespModel();
                model1.gradeId = yxyLessonsSynchroCatalog.getGradeId();
                YxyLessonsSynchroGrade yxyLessonsSynchroGrades = yxyLessonsSynchroGradeRepository
                        .findById(model1.gradeId);
                if (yxyLessonsSynchroGrades != null) {
                    model1.gradeName = yxyLessonsSynchroGrades.getName();
                }

                returnLst.add(model1);
            }
        }

        return returnLst;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiReqModel {

        // 学段id
        public String stageId;

        // 学科ID(如果点击不限此Id传空字符串)
        public String subjectId;

    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 版本ID
        public String versionId;
        // 版本名称
        public String versioName;
        // 分类ID
        public String gradeId;
        // 分类名称
        public String gradeName;
    }
}
